public with sharing class NCFileSearchController {
	public String paramsJson {get;set;}
	public String resJson {get;set;}
	public String msg {get;private set;}
    public NCFileSearchController() {
        
    }

    public void setNil(){
    	paramsJson = '';
    	resJson = '';
    	msg = '';
    }

    public void Search(){
    	msg = '';
    	Map<String,Object> params = (Map<String,Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(paramsJson, 'UTF-8'));

    	String sql = getSql();
        //System.debug(params);

    	if(String.isBlank( String.valueOf(params.get('invname')) )== false ){
    		sql += ' and d.invname like \'%'+String.valueOf(params.get('invname'))+'%\' ';
    	}
    	if(String.isBlank( String.valueOf(params.get('invcode')) )== false ){
    		sql += ' and d.invcode like \'%'+String.valueOf(params.get('invcode'))+'%\' ';
    	}
    	if(String.isBlank( String.valueOf(params.get('invmnecode')) )== false ){
    		sql += ' and d.invmnecode like \'%'+String.valueOf(params.get('invmnecode'))+'%\' ';
    	}
    	if(String.isBlank( String.valueOf(params.get('invpinpai')) )== false ){
    		sql += ' and d.invpinpai like \'%'+String.valueOf(params.get('invpinpai'))+'%\' ';
    	}
    	if(String.isBlank( String.valueOf(params.get('invspec')) )== false ){
    		sql += ' and d.invspec like \'%'+String.valueOf(params.get('invspec'))+'%\' ';
    	}
    	if(String.isBlank( String.valueOf(params.get('invtype')) )== false ){
    		sql += ' and d.invtype like \'%'+String.valueOf(params.get('invtype'))+'%\' ';
    	}

    	sql += ' and rownum <= 200 ';
    	String url = Label.queryResposityURL;

     	 sql = '{"data":" '+sql+'"}';
    	System.debug(sql);
     	 //请求

      	HttpRequest req = new HttpRequest();
      	req.setBody(sql);
      	req.setMethod('POST');
      	req.setHeader('Content-Type','text/plain');
      	req.setEndpoint(url);
      	HttpResponse res = Utils.postRequest(req, 2);
      ///返回结果处理
        if (res.getStatusCode() == 200) {
            System.debug(res.getBody());
            Map<String,Object> r = null;
              //如果是测试
                if(Test.isRunningTest()){
                  Map<String,Object>  resBody= new Map<String,Object>();
                  resBody.put('code','0');
                  resBody.put('result','{\"result_msg\":\"查询成功！\",\"result\":1,\"data\":[{\"nccode\":\"111\",\"company\":\"111\",\"djh\":\"111\",\"djdate\":\"111\",\"signdate\":\"111\",\"termaccount\":\"111\",\"payaccount\":\"111\",\"xydqr\":\"111\",\"ye\":\"111\",\"psncode\":\"111\",\"psnname\":\"111\",\"prodlinecode\":\"111\",\"prodlinename\":\"111\",\"djrq\":\"111\",\"djbh\":\"111\",\"currtypename\":\"111\"}]}');
                    r =  resBody;
                }else{
                  r= (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
                }
        	// r = (Map<String,Object>) JSON.deserializeUntyped(res.getBody());
        	if(r.get('code') == 0 || r.get('code') == '0'){
        		 Map<String,Object> rs = (Map<String,Object>) JSON.deserializeUntyped(((String)r.get('result')));
	                           System.debug(rs);

                if( rs.get('result') == 1 ){
	                List<Object> datas = (List<Object>)rs.get('data');
	               List<NCFilBean> ebList = new List<NCFilBean>();
	               for(Object ob :datas){
	                	NCFilBean rd = new NCFilBean((Map<String,Object>)ob);
	                    ebList.add(rd);
	                }
	                //System.debug('==>'+ebList);
      				resJson = EncodingUtil.urlEncode(JSON.serialize(ebList), 'UTF-8') ;
                     System.debug('==>'+resJson);
                     System.debug(EncodingUtil.urlDecode(resJson, 'UTF-8'));


	           	}else{
	           		msg = String.valueOf(rs.get('result_msg'));
	           	}

      		//resJson = EncodingUtil.urlEncode(res.getBody(), 'UTF-8') ;
		    }else{
		    	msg = '查询NC文件信息失败 : '+String.valueOf(r.get('msg'));
		    	 ///System.debug(res.toString());
		    	//resJson = EncodingUtil.urlEncode('{"result":"","code":"1","msg":"接口错误,查询失败!"}','UTF-8');
		    }

	    }else{
	    	msg = '查询NC文件信息失败 : '+res.getBody();
	    }





    }

    public String getSql(){

    	String sql = '';

	    sql +=' select distinct ';
		sql +=' d.invname , ';
		sql +=' d.invcode , ';
	   	sql +=' d.invmnecode , ';
	   	sql +=' d.invpinpai , ';
	   	sql +=' d.invspec , ';
	   	sql +=' d.invtype , ';
		sql +=' a.pk fileId, ';
        sql +='  substr(a.ts,0,10) ts ,';
        sql +=' substr(a.path, instr(a.path, \'/\', -1, 1) + 1, 1000) filename ';
	  	sql +=' from sm_pub_filesystem a ';
	  	sql +=' left outer join bd_invmandoc b ';
	    sql +=' on b.pk_invbasdoc = substr(a.path, 11, 20) ';
	   	sql +=' and b.pk_corp = substr(a.path, instr(a.path, \'_\', 1, 1) + 1, 4) ';
	  	sql +=' left outer join bd_invbasdoc c ';
	    sql +=' on c.pk_invbasdoc = substr(a.path, 11, 20) ';
	  	sql +=' left outer join bd_invbasdoc d ';
	    sql +=' on d.pk_invbasdoc = nvl(c.pk_invbasdoc, b.pk_invbasdoc) ';
		sql +=' where a.isfolder = \'n\' ';
		sql +=' and  a.path like \'存货档案_%\' ';

    	return sql ;
    }

    public class NCFilBean{
    	public String invname;
    	public String invcode ;
    	public String invmnecode;
    	public String invpinpai ;
    	public String invspec ;
    	public String invtype ;
    	public String fileId;
        public String filename ;
        public String ts;
    	public NCFilBean (Map<String,Object> e){
    		invname = String.valueOf(e.get('invname'));
    		invcode = String.valueOf(e.get('invcode'));
    		invmnecode = String.valueOf(e.get('invmnecode'));
    		invpinpai = String.valueOf(e.get('invpinpai'));
    		invspec = String.valueOf(e.get('invspec')) ;
    		invtype = String.valueOf(e.get('invtype'));
    		fileId = String.valueOf(e.get('fileid'));
            filename = String.valueOf(e.get('filename'));
            ts = String.valueOf(e.get('ts'));

    	}

    }

}